package com.xqboss.apps.controller.box;

import com.xqboss.apps.domain.param.AesParams;
import com.xqboss.apps.domain.user.UserBox;
import com.xqboss.apps.dto.user.OpenBoxDto;
import com.xqboss.apps.dto.user.OpenBoxInfoDto;
import com.xqboss.apps.dto.user.UserBoxListDto;
import com.xqboss.apps.service.user.UserBoxService;
import com.xqboss.apps.util.mh.BeanCopierUtil;
import com.xqboss.apps.vo.box.AppUserBoxVo;
import com.xqboss.apps.vo.box.OpenBoxInfoVo;
import com.xqboss.apps.vo.box.UserBoxInfoVo;
import com.xqboss.apps.vo.user.UserNFTItemVo;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.exception.ServiceException;
import com.xqboss.common.utils.ValidatorUtil;
import com.xqboss.common.utils.aes.AesUtil;
import com.xqboss.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author Administrator
 */

@Api(tags = {"用户宝箱管理"})
@RestController
@RequestMapping("/apps/user-box")
public class UserBoxController extends BaseController {

    @Autowired
    private UserBoxService userBoxService;

    @Autowired
    private ISysUserService sysUserService;

    @ApiOperation("获取宝箱列表")
    @GetMapping("box-list")
    public R<List<AppUserBoxVo>> boxList(@Validated UserBoxListDto request) {
        request.startPage();
        R<List<AppUserBoxVo>> result = userBoxService.getUserBoxList(getUserId());
        return result;
    }

    @ApiOperation("获取某个箱子信息")
    @GetMapping("my-box/{boxId}")
    public R<AppUserBoxVo> boxList(@Validated @PathVariable("boxId") @NotNull(message = "请输入箱子ID") Long boxId) {
        UserBox userBox = userBoxService.getUserBoxByBoxId(getUserId(), boxId);
        AppUserBoxVo vo = BeanCopierUtil.copy(userBox, AppUserBoxVo.class);
        return R.ok(vo);
    }

    @ApiOperation("开箱弹窗信息")
    @GetMapping("open-box-info")
    public R<OpenBoxInfoVo> openBoxInfo(@Validated OpenBoxInfoDto request) {
        OpenBoxInfoVo vo = userBoxService.getOpenBoxInfo(getUserId(), request);
        return R.ok(vo);
    }


    @RepeatSubmit(interval = 1000, justUrl = true)
    @ApiOperation("开箱")
    @PostMapping("open-box")
    public R<List<UserNFTItemVo>> openBox(@Validated @RequestBody AesParams aesParams) {
        OpenBoxDto request = AesUtil.decryptToObject(aesParams.getParamStr(), OpenBoxDto.class);
        ValidatorUtil.valid(request);
        sysUserService.checkPayPwd(getUserId(), request.getPayPassword());
        List<UserNFTItemVo> nftList = userBoxService.openBox(getUserId(), request);
        return R.ok(nftList);
    }


    @ApiOperation("宝箱详情")
    @GetMapping("box-detail/{userBoxId}")
    public R<UserBoxInfoVo> boxDetail(@Validated @PathVariable("userBoxId")
                                      @ApiParam(value = "宝箱ID", required = true)
                                      @NotNull(message = "用户宝箱ID不能为空")
                                      Long userBoxId) {
        UserBoxInfoVo vo = userBoxService.getBoxDetail(userBoxId);
        return R.ok(vo);
    }
}
